package com.cs410.collabwriting.main;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.cs410.collabwriting.tools.DatabaseManager;
import com.cs410.collabwriting.tools.queryTools.DatabaseTable;
import com.cs410.collabwriting.tools.queryTools.QueryBuilder;
import com.cs410.collabwriting.tools.queryTools.QueryCondition;
import com.cs410.collabwriting.tools.queryTools.QueryType;
import com.cs410.collabwriting.tools.queryTools.tables.POST_TABLE;

public class Post implements DatabaseObjects {

	public int m_postID = -1;
	public int m_storyID = 0;
	public String m_userID = null; 
	public String m_post = null;
	public String m_timestamp = null;

	
	public Post() {
		
	}
	
	public Post( int postID ) {
		m_postID = postID;
		ArrayList<String[]> results = DatabaseManager.executeQueryWithColumnNames(
			new QueryBuilder(QueryType.SELECTION)
			.addTables(DatabaseTable.POST_TABLE)
			.addConditions(QueryCondition.AND, POST_TABLE.POSTID, QueryCondition.EQ, postID)
			.value() );
		if( results != null ) {
			m_storyID = Integer.parseInt(DatabaseManager.getFirstValueInColumn( results, POST_TABLE.STORYID ).trim() );
			m_userID = DatabaseManager.getFirstValueInColumn( results, POST_TABLE.USERID );
			m_post = DatabaseManager.getFirstValueInColumn( results, POST_TABLE.POST );
			m_timestamp = DatabaseManager.getFirstValueInColumn( results, POST_TABLE.TIMESTAMP );
		}
	}
	

	
	public int getPostID() {
		return m_postID;
	}
	
	public String getPost() {
		return m_post;
	}
	
	public String getTimeStamp() {
		if( m_timestamp == null && m_postID > 0 ) {
			m_timestamp = DatabaseManager.executeExactQuery(
				new QueryBuilder(QueryType.SELECTION)
				.addColumns(POST_TABLE.TIMESTAMP)
				.addTables(DatabaseTable.POST_TABLE)
				.addConditions(QueryCondition.AND, POST_TABLE.POSTID, QueryCondition.EQ, m_postID)
				.value() );
		}
		
		if( m_timestamp == null ) {
			m_timestamp = new Date().toString();
		}
		SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");
		
        try {
            Date date = parser.parse(m_timestamp);
            return formatter.format(date);
        } catch (ParseException e) {            
            return m_timestamp.substring(0, m_timestamp.length()-2);
        }        
	}
	
	public String getTimeStampinNum(){
		if( m_timestamp == null && m_postID > 0 ) {
			m_timestamp = DatabaseManager.executeExactQuery(
				new QueryBuilder(QueryType.SELECTION)
				.addColumns(POST_TABLE.TIMESTAMP)
				.addTables(DatabaseTable.POST_TABLE)
				.addConditions(QueryCondition.AND, POST_TABLE.POSTID, QueryCondition.EQ, m_postID)
				.value() );
		}
		
		if( m_timestamp == null ) {
			m_timestamp = new Date().toString();
		}		

        return m_timestamp.substring(0, m_timestamp.length()-2);  
	}
	
	public String getUserID() {
		return m_userID;
	}
	
	
	
	@Override
	public void update() {
//		m_storyID = 0;
//		m_post = null;
//		m_timestamp = null;
//		m_userID = null;
		if( m_postID > 0 ) {
			ArrayList<String[]> results = DatabaseManager.executeQueryWithColumnNames(
					new QueryBuilder(QueryType.SELECTION)
					.addTables(DatabaseTable.POST_TABLE)
					.addConditions(QueryCondition.AND, POST_TABLE.POSTID, QueryCondition.EQ, m_postID )
					.value() );
			if( results != null ) {
				m_storyID = new Integer(DatabaseManager.getFirstValueInColumn( results, POST_TABLE.STORYID ) );
				m_userID = DatabaseManager.getFirstValueInColumn( results, POST_TABLE.USERID );
				m_post = DatabaseManager.getFirstValueInColumn( results, POST_TABLE.POST );
				m_timestamp = DatabaseManager.getFirstValueInColumn( results, POST_TABLE.TIMESTAMP );
			}
		}
		
	}

	
}
